package com.lepu.app.fun.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.UIHelper;
import com.eyzhs.app.R;
import com.lepu.app.usercenter.util.UserCenterUtil;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * creat by weichyang 2015/10/19
 * 举报
 */

public class ReportActivity extends BaseFragmentActivity implements
        OnTopbarNewLeftLayoutListener, CustomTopBarNew.OnTopbarNewRightButtonListener, AsyncRequest, View.OnClickListener {
    private static final String REPORT_REQUEST = "report";
    private static final String TARGETNAME_REQUEST = "TargetName";
    private int mReportType;
    private String mReportID;
    private RadioGroup mRadioGroup;
    private String mTargetName;
    private TextView mTxtBuchong;
    private RelativeLayout mRelatnewNotice;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_report_activity);
        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 添加页面动画，需要重写这个方法，
     */
    @Override
    public void onBackPressed() {
        finish(true);
    }

    @Override
    public void onTopbarLeftLayoutSelected() {
        finish(true);
    }

    //===================================================================

    /**
     * 初始化方法
     */
    private void init() {
        mReportType = getIntent().getIntExtra("reportType", 0);
        mReportID = getIntent().getStringExtra("reportID");
        mTargetName = getIntent().getStringExtra("reportName");
        CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
        topbar.setTopbarTitle(R.string.reportuser);
        topbar.setonTopbarNewLeftLayoutListener(this);
        topbar.setRightText(R.string.reportuser);
        topbar.setOnTopbarNewRightButtonListener(this);
        mRelatnewNotice = (RelativeLayout) findViewById(R.id.relintroduce);
        mRadioGroup = (RadioGroup) findViewById(R.id.radio_report);
        mTxtBuchong = (TextView) findViewById(R.id.txt_buchong);
        mRelatnewNotice.setOnClickListener(this);
    }

    @Override
    public void onTopbarRightButtonSelected() {
        int reasonCode = selectRadioButton();
        if (reasonCode == 0) {
            UIHelper.showToast(this, (String) getText(R.string.please_select_report_rea));
            return;
        }
        UserCenterUtil.useReport(this, REPORT_REQUEST, mReportType, mReportID, reasonCode, "", mTargetName);
    }

    /**
     * 判断举报选择的理由
     *
     * @return
     */
    private int selectRadioButton() {
        int reasonCode = 0;
        switch (mRadioGroup.getCheckedRadioButtonId()) {
            case R.id.rb_item1:
                reasonCode = 1;
                break;
            case R.id.rb_item2:
                reasonCode = 2;
                break;
            case R.id.rb_item3:
                reasonCode = 3;
                break;
            case R.id.rb_item4:
                reasonCode = 4;
                break;
            case R.id.rb_item5:
                reasonCode = 5;
                break;
            default:
                reasonCode = 0;
                break;
        }
        return reasonCode;
    }

    @Override
    public void RequestComplete(Object o, Object o1) {
        finish(true);
    }

    @Override
    public void RequestError(Object o, int i, String s) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject((String) s);
            String friendMeterial = jsonObject.optJSONObject("Result").optString("Message");
            UIHelper.showToast(this, friendMeterial);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.relintroduce: //选填内容
                Intent intent = new Intent(this, ReportContentActivity.class);
                startActivityForResult(intent, 100, true);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //回显
        if (requestCode == 100 && resultCode == RESULT_OK) {
            String remarkName = data.getStringExtra("content");
            if (!TextUtils.isEmpty(remarkName))
                mTxtBuchong.setText(remarkName);
        }

    }
}
